﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Diagnostics.Contracts;

namespace Numero4DBxml
{
    public partial class Form1 : Form
    {
        private ConnexionDB connexion;

        private const string CHEMIN_ETUDIANT = @"Etudiants.xml";
        private const string CHEMIN_COURS = @"Cours.xml";
        private const string CHEMIN_RESULTAT = @"Resultats.xml";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            connexion = ConnexionDB.Creer();

            connexion.DBtoXML("SELECT * FROM cours", CHEMIN_COURS);
            connexion.DBtoXML("SELECT * FROM resultats", CHEMIN_RESULTAT);
            connexion.DBtoXML("SELECT * FROM etudiants", CHEMIN_ETUDIANT);

            txtCoursTW.Text = File.ReadAllText(CHEMIN_COURS);
            txtResultatTW.Text = File.ReadAllText(CHEMIN_RESULTAT);
            txtEtudiantTW.Text = File.ReadAllText(CHEMIN_ETUDIANT);
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            fillText(txt1);
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            fillText(txt2);
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            fillText(txt3);
        }

        private void fillText(TextBox tb)
        {
            Contract.Requires(tb != null, "Le textbox de destination est null.");
            
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (!String.IsNullOrEmpty(ofd.FileName))
                {
                    try
                    {
                        tb.Text = File.ReadAllText(ofd.FileName);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Erreur au chargement du fichier; " + e.Message);
                    }
                   
                }
            }

            ofd.FileName = String.Empty;
        }
    }
}
